<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="图书编号" prop="informbooknum">
      <el-input v-model="dataForm.informbooknum" placeholder="图书编号"></el-input>
    </el-form-item>
    <el-form-item label="借阅证编号" prop="informcertnum">
      <el-input v-model="dataForm.informcertnum" placeholder="借阅证编号"></el-input>
    </el-form-item>
      <el-form-item label="借阅日期" prop="informdate">
        <el-date-picker
          v-model="dataForm.informdate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="应归还日期" prop="informreturndate">
        <el-date-picker
          v-model="dataForm.informreturndate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际归还日期" prop="informrealdate">
        <el-date-picker
          v-model="dataForm.informrealdate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借阅状态" prop="informstatus">
        <el-select v-model="dataForm.informstatus" placeholder="请选择">
          <el-option label="已归还" value="已归还"></el-option>
          <el-option label="未归还" value="未归还"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          informbooknum: '',
          informcertnum: '',
          informdate: '',
          informreturndate: '',
          informrealdate: '',
          informstatus: ''
        },
        dataRule: {
          informbooknum: [
            { required: true, message: '图书编号不能为空', trigger: 'blur' }
          ],
          informcertnum: [
            { required: true, message: '借阅证编号不能为空', trigger: 'blur' }
          ],
          informdate: [
            { required: true, message: '借阅日期不能为空', trigger: 'blur' }
          ],
          informreturndate: [
            { required: true, message: '应归还日期不能为空', trigger: 'blur' }
          ],
          informstatus: [
            { required: true, message: '借阅状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bs_borrinform/bsborrinform/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.informbooknum = data.bsBorrinform.informbooknum
                this.dataForm.informcertnum = data.bsBorrinform.informcertnum
                this.dataForm.informdate = data.bsBorrinform.informdate
                this.dataForm.informreturndate = data.bsBorrinform.informreturndate
                this.dataForm.informrealdate = data.bsBorrinform.informrealdate
                this.dataForm.informstatus = data.bsBorrinform.informstatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bs_borrinform/bsborrinform/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'informbooknum': this.dataForm.informbooknum,
                'informcertnum': this.dataForm.informcertnum,
                'informdate': this.dataForm.informdate,
                'informreturndate': this.dataForm.informreturndate,
                'informrealdate': this.dataForm.informrealdate,
                'informstatus': this.dataForm.informstatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
